﻿using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
using System.Management;
using System.Text;
using System.Net.Mail;

public partial class UserDetail : System.Web.UI.Page
{
    Microsoft.Practices.EnterpriseLibrary.Data.Database db = DatabaseFactory.CreateDatabase("Application_Data");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {            
            if (Convert.ToBoolean(Session["sAdministrator"].ToString()) == false)
            {
                this.isAdministrator.Visible = false;
                this.btnDelete.Visible = false;
            }
            else //You are an Administrator
            {
                this.isAdministrator.Visible = true;
                this.btnDelete.Visible = true;
                this.btnDelete.Attributes.Add("OnCLick", "return confirm('Are you sure you want to delete this User?');");
            }

            DDLTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();
            DDLTimeZone.DataValueField = "Id";
            DDLTimeZone.DataTextField = "DisplayName";
            DDLTimeZone.DataBind();

            BindData();
        }
    }
    private void BindData()
    {
        DbCommand dbCommand1 = db.GetSqlStringCommand("SELECT * FROM [User] WHERE UserDomain = '" + Request.QueryString["UserDomain"].ToString() + "' AND UserNetworkLogin = '" + Request.QueryString["UserNetworkLogin"].ToString() + "'");
        DataSet ds1 = db.ExecuteDataSet(dbCommand1);
        DataRow dr1 = ds1.Tables[0].Rows[0];

        this.lblUserDomain.Text = dr1["UserDomain"].ToString();
        this.lblUserNetworkLogin.Text = dr1["UserNetworkLogin"].ToString();
        this.lblUserFullName.Text = dr1["UserFullName"].ToString();
        this.lblUserEMailAddress.Text = dr1["UserEMailAddress"].ToString();
        this.DDLTimeZone.Items.FindByValue(dr1["TimeZone"].ToString()).Selected = true;
        this.isAdministrator.Checked = Convert.ToBoolean(dr1["isAdmin"].ToString());
        this.lblCreatedDate.Text = Convert.ToDateTime(dr1["CreatedDate"]).ToShortDateString();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("DELETE FROM [User] WHERE UserDomain = '" + this.lblUserDomain.Text.ToString() + "' AND UserNetworkLogin = '" + this.lblUserNetworkLogin.Text.ToString() + "'");
        db.ExecuteNonQuery(dbCommand);

        Response.Redirect("~/SiteAdministration.aspx");
    }
    protected void isAdministrator_CheckedChanged(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE [User] SET isAdmin = '" + Convert.ToBoolean(this.isAdministrator.Checked) + "' WHERE UserDomain = '" + this.lblUserDomain.Text.ToString() + "' AND UserNetworkLogin = '" + this.lblUserNetworkLogin.Text.ToString() + "'");
        db.ExecuteNonQuery(dbCommand);

        MessageBox.Show("Update Successful.");
    }
    protected void DDLTimeZone_SelectedIndexChanged(object sender, EventArgs e)
    {
        DbCommand dbCommand = db.GetSqlStringCommand("UPDATE [User] SET TimeZone = '" + DDLTimeZone.SelectedValue.ToString() + "', TimeZoneDisplayName = '" + this.DDLTimeZone.SelectedItem.ToString() + "' WHERE UserDomain = '" + this.lblUserDomain.Text.ToString() + "' AND UserNetworkLogin = '" + this.lblUserNetworkLogin.Text.ToString() + "'");
        db.ExecuteNonQuery(dbCommand);

        MessageBox.Show("Update Successful.");
    }
}
